package com.seeyon.bugfixtools.workflow.dao;

import com.seeyon.bugfixtools.workflow.dto.ColSummary;
import com.seeyon.bugfixtools.workflow.dto.WarningAuthDto;
import com.seeyon.bugfixtools.workflow.dto.WorkflowTemplateInfo;
import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class WorkflowAuthDao {

    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(WorkflowAuthDao.class);

    @Lazy
    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 获取有问题的权限数据
     *
     * @return
     */
    public List<WarningAuthDto> getWarningAuth() {
        String sql = "select idm.id authMappingId,old.resource_id oldResourceId,idm.view_id viewId,idm.auth_resource_id authResourceId " +
                " from CAP_FORM_AUTH_ID_MAPPING idm " +
                " left join CAP_FORM_AUTH_INFO old on old.old_resource_id = idm.auth_resource_id " +
                " left join CAP_FORM_AUTH_INFO new on new.resource_id = idm.auth_resource_id " +
                " where new.id is null";
        return jdbcTemplate.queryForList(sql, WarningAuthDto.class);
    }

    /**
     * 根据资源ID和视图ID获取权限映射数据
     *
     * @param resourceId
     * @param viewId
     */
    public void deleteAuthMappingByResourceIdAndViewId(Long resourceId, Long viewId) {
        String sql = "delete from CAP_FORM_AUTH_ID_MAPPING where auth_resource_id = ? and view_id = ?";
        jdbcTemplate.update(sql, resourceId, viewId);
        logger.info("delete sql:" + sql);
    }

    public void updateAuthMappingResourceIdById(Long resourceId, Long id) {
        String sql = "update CAP_FORM_AUTH_ID_MAPPING set auth_resource_id = ?  where id = ? " ;
        jdbcTemplate.update(sql,resourceId,id);
        logger.info("update sql:" + sql);
    }

    public void deleteAuthInfoByResourceId(Long resourceId) {
        String sql = "delete from CAP_FORM_AUTH_INFO where resource_id  = ? " ;
        logger.info("delete sql:" + sql);
        jdbcTemplate.update(sql,resourceId);
    }


    /**
     * 获取流程模板数据
     * @return
     */
    public List<WorkflowTemplateInfo> getAllTemplateInfo() {
        String sql = " SELECT " +
                " f.id formId, " +
                " f.NAME formName, " +
                " t.id templateId, " +
                " t.SUBJECT templateName, " +
                " t.WORKFLOW_ID workflowTemplateId, " +
                " wt.WORKFLOW workflowXml " +
                "FROM " +
                " cap_form_definition f " +
                " LEFT JOIN ctp_template t ON f.id = t.FORM_APPID " +
                " left join wf_process_templete wt on wt.id = t.WORKFLOW_ID";
        return jdbcTemplate.query(sql, (rs, rowNum) -> {
            WorkflowTemplateInfo dto = new WorkflowTemplateInfo();
            // 手动映射查询结果到 ScanLockResDto 类的实例
            dto.setFormId(rs.getString("formId"));
            dto.setFormName(rs.getString("formName"));
            dto.setTemplateId(rs.getString("templateId"));
            dto.setTemplateName(rs.getString("templateName"));
            dto.setWorkflowTemplateId(rs.getString("workflowTemplateId"));
            dto.setWorkflowXml(rs.getString("workflowXml"));
            return dto;
        });
    }


    public void getTemplateRunningWarningAuth() {

        String sql = "select id id,subject subject,STATE state,TEMPLETE_ID templateId,PROCESS_ID processId from col_summary where state <> 3 ";
        List<ColSummary> colSummaries = jdbcTemplate.queryForList(sql, ColSummary.class);
    }
}
